package com.halflife.comeElectricity.orderMng.order.dto;

import com.alibaba.fastjson.JSON;
import com.halflife.framework.utils.Utils;
import com.halflife.comeElectricity.basic.goodsType.GoodsType;
import com.halflife.comeElectricity.orderMng.order.Order;
import com.halflife.comeElectricity.orderMng.order.OrderEnum;
import com.halflife.comeElectricity.orderMng.order.OrderExt;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * Created by zhaoyf on 2018/2/12.
 */
public class OrderDTO {
    public String startTime ;
    public GoodsType goodsType;
    private Integer id;
    private SiteDTO startPlace;//起步地
    private List<SiteDTO> destination;//目的地
    private String extractService;//额外服务
    private String toDriver;//给司机捎话
    private BigDecimal amount;//总额
    private BigDecimal fee;//小费
    private BigDecimal distance;//距离
    private BigDecimal serviceMoney;
    private String payType;
    private String openId;


    public void convert(Order order, OrderExt ext)throws Exception {
        if (this.startPlace!=null) {
            ext.setStartPlace(JSON.toJSONString(this.startPlace));
            order.setStart(this.getStartPlace().getUserAddress()+ Utils.getString(this.getStartPlace().getUserAddressDetail()));
            order.setLatitude(Double.valueOf(this.startPlace.getLocationLat()));
            order.setLongitude(Double.valueOf(this.startPlace.getLocationLan()));

        }
        if (this.destination!=null) {
            //将距离最远的目的地保存到订单目的地 edit by liuj3
            //计数器
            int num = 0;
            //order.setEnd(this.getDestination().get(this.getDestination().size()-1).getUserAddress()+this.getDestination().get(this.getDestination().size()-1).getUserAddressDetail());
            if(this.destination.size()>0){
                Collections.sort(this.destination);
                SiteDTO des = this.destination.get(this.destination.size() - 1);
                order.setEnd(des.getUserAddress()+ Utils.getString(des.getUserAddressDetail()));
            }
            ext.setDestination(JSON.toJSONString(this.destination));
        }
        ext.setExtractService(this.getExtractService());
        ext.setToDriver(this.getToDriver());
        order.setRiderAmount(this.getAmount().add(this.getFee()).subtract(this.serviceMoney));
        ext.setFee(this.getFee());
        ext.setAmount(this.getAmount());
        ext.setDistance(this.getDistance());
        order.setStatus(OrderEnum.NOPAY.m_orderStatus.byteValue());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        order.setStartTime(sdf.parse(this.getStartTime()).getTime());
        order.setCreateTime(Calendar.getInstance().getTimeInMillis());
        order.setGoodTypeId(this.goodsType.getId());
        order.setGoodsType(this.goodsType.getName());
        order.setTotalAmount(this.getAmount().add(this.getFee()));
        order.setFee(this.getFee());
    }

    public BigDecimal getServiceMoney() {
        return serviceMoney;
    }

    public void setServiceMoney(BigDecimal serviceMoney) {
        this.serviceMoney = serviceMoney;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public GoodsType getGoodsType() {
        return goodsType;
    }

    public void setGoodsType(GoodsType goodsType) {
        this.goodsType = goodsType;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public SiteDTO getStartPlace() {
        return startPlace;
    }

    public void setStartPlace(SiteDTO startPlace) {
        this.startPlace = startPlace;
    }

    public List<SiteDTO> getDestination() {
        return destination;
    }

    public void setDestination(List<SiteDTO> destination) {
        this.destination = destination;
    }

    public String getExtractService() {
        return extractService;
    }

    public void setExtractService(String extractService) {
        this.extractService = extractService;
    }

    public String getToDriver() {
        return toDriver;
    }

    public void setToDriver(String toDriver) {
        this.toDriver = toDriver;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public BigDecimal getFee() {
        return fee;
    }

    public void setFee(BigDecimal fee) {
        this.fee = fee;
    }

    public BigDecimal getDistance() {
        return distance;
    }

    public void setDistance(BigDecimal distance) {
        this.distance = distance;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }
}
